define(['jquery',
     'jqureyui',
    'underscore',
    'handlebar',
    'backbone',   
    '../../collection/category/Ccategory',   
    'text!../../../templates/common/headerSearchBox.html',
],
        function ($,
                jqueryui,
                _,
                Handlebar,
                Backbone,
                CollectoionCategory,               
                headerSearchBox
                ) {
            var arrAvailableProducts = [];
            var VItemsRow = Backbone.View.extend({               
                el: $('#left-cat-navigation'),               
                initialize: function () {                    
                   
                    var that = this;
                    var collectoionCategory = new CollectoionCategory();
                   
                    collectoionCategory.fetch({
                        
                        success: function (collectoionCategory) {
                           // debugger;
                            that.render(collectoionCategory);
                        },
                        error: function(err) {                           
                            console.log("%c ERROR: in fetching categories","color:red,font-size:samll",err)
                        }
                    });
                },
                render: function (collectoionCategory) {
                    var self = this;
                    //Filling Header Seerch Box
                    var retJSON = collectoionCategory.toJSON();

                    //removing dupicate city from JSON object
                    var duplicateFreeCityArr = _.uniq(retJSON, function (item) {
                        return item.city;
                    });

                    var headerSerchTemplate = Handlebars.compile(headerSearchBox);
                    var headerSerchTemplateDOM = headerSerchTemplate(duplicateFreeCityArr);
                    $('#searchBox').html(headerSerchTemplateDOM);
                    arrAvailableProducts = [];
                    _.map(retJSON, function (value, key) {
                        //updating autocomplete arra wtih product name and brand name
                        arrAvailableProducts.push(value.name)
                        arrAvailableProducts.push(value.brandName)
                        arrAvailableProducts.push(value.retailerName)
                    });

                    
                    //removing duplicate items from an array using uderscire UNIQ method
                    var duplicateFreeProdArr = _.uniq(arrAvailableProducts, function (item) {
                        return item;
                    });


                    //Auto complete Search Box
                    $("#searchItem").autocomplete({
                        source: duplicateFreeProdArr,
                        close: function (event, ui) {
                            alert('asda');
                            var vheader = new VHeader();
                            window.searchItem = event.target.value;
                            vheader.searchProducts(event.target.value);
                        }
                    });
                    
                }                
            });
            return VItemsRow;
        });

